<template>
  <div class="proption-chart">
    <div class="title">
      {{ proptionTitle }}
    </div>
    <div class="chart" ref="ProptionChartDom"></div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, onMounted, watch } from 'vue'
const props = defineProps({
  proptionData: {
    type: Array,
    default: () => []
  },
  proptionTitle: {
    type: String,
    default: ''
  }
})
const ProptionChartDom = ref(null)
const { proxy } = getCurrentInstance()
const option = reactive({
  tooltip: {
    trigger: 'item',
    backgroundColor: '#666',
    borderColor: '#666',
    textStyle: {
      color: '#fff'
    }
  },
  series: [
    {
      name: '',
      type: 'pie',
      radius: '50%',
      data: [],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
})
const ProptionChart = ref(null)
const initChart = () => {
  ProptionChart.value = proxy.$echarts.init(ProptionChartDom.value)
  updateOption(option)
}
const updateOption = (option) => {
  option.series[0].data = props.proptionData
  ProptionChart.value?.setOption(option)
}
watch(
  () => props.proptionData,
  () => {
    updateOption(option)
  }
)
onMounted(() => {
  initChart()
})
</script>

<style lang="less" scoped>
.proption-chart {
  width: 100%;
  height: 300px;
  color: #fff;
  background: url('@/assets/image/chart-bg.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  .title {
    height: 40px;
    line-height: 40px;
    font-size: 18px;
    font-weight: 650;
    color: #01e4f8;
    text-align: center;
  }
  .chart {
    flex: 1;
  }
}
</style>
